<template>
    <mi-form-page @submit="submit"
                  :loading="edit.loading"
                  title="文章编辑"
                  v-model:show="edit.show"
    >
        <el-form :model="edit.info"
                 :rules="rules"
                 ref="formRef">
            <el-form-item prop="image" label="图片">
                <mi-up-image v-model="edit.info.image"/>
            </el-form-item>
            <el-form-item prop="cate_id" label="分类">
                <article-cate-select v-model="edit.info.cate_id" />
            </el-form-item>
            <el-form-item prop="title" label="标题">
                <mi-input v-model="edit.info.title"/>
            </el-form-item>

            <el-form-item prop="content" label="内容">
                <mi-wang-editor v-model="edit.info.content" />
            </el-form-item>
            <el-form-item prop="visit_vir" label="虚拟浏览量">
                <mi-input-number v-model="edit.info.visit_vir"  />
            </el-form-item>
            <el-form-item prop="show_type" label="显示">
                <mi-radio v-model="edit.info.show_type"  />
            </el-form-item>
        </el-form>
    </mi-form-page>

</template>

<script setup lang="ts">
import {reactive, watch, ref, onBeforeMount, inject} from "vue";
import {simpleGet} from "@/utils/request";
import {makeModelAuth} from "@/utils/auth";
import {FormInstance} from "element-plus";
import List from "@/model/list";
import Edit from "@/model/edit";
import {useAdminStore} from "@/store/admin";
import ArticleCateSelect from "@/views/site/components/ArticleCateSelect.vue";

const auth = makeModelAuth("site.article")
const rules = {
    title:[{required:true, message:'必填'}],
    cate_id:[{required:true, message:'必填'}],
    content:[{required:true, message:'必填'}],
}
const adminStore = useAdminStore()
const formRef = ref<FormInstance>()
const list = inject<List>("list")
const edit = inject<Edit>("edit", new Edit({}))

const submit = () => {
    let url = '/' + (list?.currentRow?.id ? auth.edit : auth.add)
    edit.submit({
        formRef: formRef.value,
        url:url,
        list: list
    })
}

watch(() => edit.show, (nv,ov) => {
    if (nv){
        edit.init(async () => {
            const id = list?.currentRow?.id
            if (id){
                // 请求表单数据
                let res = await simpleGet('/' + auth.edit, {id})
                edit.info = res.info
            }else{
                edit.info = {
                    is_show:1,
                    visit_vir:0
                }
            }
        })
    }
})


</script>

<style scoped>

</style>
